/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.token.dao.CompanyDAO;
import com.opgea.token.dao.DestinationDAO;
import com.opgea.token.domain.entities.Company;
import com.opgea.token.domain.entities.Destination;
import com.opgea.token.service.DestinationService;
import com.opgea.token.web.dto.DestinationDTO;

/**
 * 
 * @author Ramesh
 */
@Service
@Transactional
public class DestinationServiceImpl implements DestinationService {

    @Autowired
    private DestinationDAO destinationDAO;
    @Autowired
    private CompanyDAO companyDAO;

    @Override
    public DestinationDTO create(DestinationDTO destinationDTO) {
	Company company = companyDAO.find(destinationDTO.getCompanyId());
	Destination destination = new Destination();
	destinationDTO.unmarshall(destination);
	destination.setCompany(company);
	destinationDAO.create(destination);
	return destinationDTO;
    }

    @Override
    public DestinationDTO update(DestinationDTO destinationDTO) {
	Destination destination = null;
	if (destinationDTO.getId() != null && destinationDTO.getId() > 0) {
	    destination = destinationDAO.find(destinationDTO.getId());
	    destinationDTO.unmarshall(destination);
	    destinationDAO.update(destination);
	} else {
	    destination = new Destination();
	    this.create(destinationDTO);
	}
	return destinationDTO;
    }

    @Override
    public DestinationDTO find(DestinationDTO destinationDTO) {
	Destination destination = destinationDAO.find(destinationDTO.getId());
	destinationDTO.marshall(destination);
	return destinationDTO;
    }

    @Override
    public List<DestinationDTO> findAll() {
	List<Destination> destinationList = destinationDAO.findAll();
	List<DestinationDTO> destDTOList = new ArrayList<DestinationDTO>();
	for (Destination dest : destinationList) {
	    DestinationDTO dto = new DestinationDTO();
	    dto.marshall(dest);
	    destDTOList.add(dto);
	}
	return destDTOList;
    }

    @Override
    public List<DestinationDTO> findAllByCompany(Long companyId) {
	List<Destination> destinationList = destinationDAO
		.findAllByCompany(companyId);
	List<DestinationDTO> destDTOList = new ArrayList<DestinationDTO>();
	for (Destination dest : destinationList) {
	    DestinationDTO dto = new DestinationDTO();
	    dto.marshall(dest);
	    destDTOList.add(dto);
	}
	return destDTOList;
    }
}
